package io.renren.modules.api.controller;


import com.alibaba.fastjson.JSONArray;
import io.renren.common.utils.R;
import io.renren.modules.api.annotation.AuthIgnore;
import io.renren.modules.api.annotation.LoginDriver;
import io.renren.modules.api.annotation.LoginUser;
import io.renren.modules.api.entity.PsOrderRouteSectionEntity;
import io.renren.modules.api.entity.TokenEntity;
import io.renren.modules.api.entity.UserEntity;
import io.renren.modules.driver.entity.SjDriverEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * API测试接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:47
 */
@RestController
@RequestMapping("/api")
public class ApiTestController {

    /**
     * 司机接口编码模板
     */
    @PostMapping("tempDriver")
    public R tempDriver(@LoginDriver SjDriverEntity driver){


        return R.ok();
    }

    /**
     * 用户接口编码模板
     */
    @PostMapping("tempUser")
    public R tempUser(@LoginUser UserEntity user){


        return R.ok();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("userInfo")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    /**
     * 测试获取司机信息
     */
    @GetMapping("driverInfo")
    public R driverInfo(@LoginDriver SjDriverEntity driver){
        return R.ok().put("driver", driver);
    }

    /**
     * 忽略Token验证测试
     */
    @AuthIgnore
    @GetMapping("notToken")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。")
                .put("测试jenkins神不神","哈哈哈哈哈，大江真棒!!!!!")
                .put("linux下jenkins使用成功","给自己点个赞");
    }

    /**
     * 接收JSON数据
     */
    @PostMapping("jsonData")
    public R jsonData(@LoginUser UserEntity user, @RequestBody TokenEntity token){
        return R.ok().put("user", user).put("token", token);
    }

    @GetMapping("getJsonData")
    public R getJsonData(@LoginUser UserEntity user, @RequestBody TokenEntity token){
        return R.ok().put("user", user).put("token", token);
    }

    /**
     * 测试只传目的地，后台来拼接路段对象,带4个start参数
     */
    @AuthIgnore
    @PostMapping("testOnlyDestination")
    public R testOnlyDestination(String psOrderRouteSections,
                                 String startName,String startNameDetail,String startLng,String startLat) {
        //将接受的json字符串转成java对象
        List<PsOrderRouteSectionEntity> psOrderRouteSectionList =
                JSONArray.parseArray(psOrderRouteSections,PsOrderRouteSectionEntity.class);
        for (int i=0 ; i < psOrderRouteSectionList.size();i++) {
            if (i == 0) {
               psOrderRouteSectionList.get(i).setStartName(startName);
               psOrderRouteSectionList.get(i).setStartNameDetail(startNameDetail);
               psOrderRouteSectionList.get(i).setStartLng(startLng);
               psOrderRouteSectionList.get(i).setStartLat(startLat);
            } else {
                psOrderRouteSectionList.get(i).setStartName(psOrderRouteSectionList.get(i-1).getEndName());
                psOrderRouteSectionList.get(i).setStartNameDetail(psOrderRouteSectionList.get(i-1).getEndNameDetail());
                psOrderRouteSectionList.get(i).setStartLng(psOrderRouteSectionList.get(i-1).getEndLng());
                psOrderRouteSectionList.get(i).setStartLat(psOrderRouteSectionList.get(i-1).getEndLat());
            }
        }

        return R.ok().put("psOrderRouteSectionList",psOrderRouteSectionList);
    }

    /**
     * 测试只传目的地，后台来拼接路段对象,没start参数，没start数据
     */
    @AuthIgnore
    @PostMapping("testOnlyDestinationNoStart")
    public R testOnlyDestinationNoStart(String psOrderRouteSections) {
        //将接受的json字符串转成java对象
        List<PsOrderRouteSectionEntity> psOrderRouteSectionList =
                JSONArray.parseArray(psOrderRouteSections,PsOrderRouteSectionEntity.class);
        for (int i=0 ; i < psOrderRouteSectionList.size();i++) {
            if (i > 0){
                psOrderRouteSectionList.get(i).setStartName(psOrderRouteSectionList.get(i-1).getEndName());
                psOrderRouteSectionList.get(i).setStartNameDetail(psOrderRouteSectionList.get(i-1).getEndNameDetail());
                psOrderRouteSectionList.get(i).setStartLng(psOrderRouteSectionList.get(i-1).getEndLng());
                psOrderRouteSectionList.get(i).setStartLat(psOrderRouteSectionList.get(i-1).getEndLat());
                psOrderRouteSectionList.get(i).setSortNum(psOrderRouteSectionList.get(i).getSortNum()-1);
            }

        }
        psOrderRouteSectionList.remove(0);

        return R.ok().put("psOrderRouteSectionList",psOrderRouteSectionList);
    }

    /**
     * 测试只传目的地，后台来拼接路段对象,没start参数，有start数据
     */
    @AuthIgnore
    @PostMapping("testOnlyDestinationHaveStart")
    public R testOnlyDestinationHaveStart(String psOrderRouteSections) {
        //将接受的json字符串转成java对象
        List<PsOrderRouteSectionEntity> psOrderRouteSectionList =
                JSONArray.parseArray(psOrderRouteSections,PsOrderRouteSectionEntity.class);
        for (int i=0 ; i < psOrderRouteSectionList.size();i++) {
            if ( i > 0 ) {
                if (i == 1) {
                    psOrderRouteSectionList.get(i).setStartName(psOrderRouteSectionList.get(i - 1).getStartName());
                    psOrderRouteSectionList.get(i).setStartNameDetail(psOrderRouteSectionList.get(i - 1).getStartNameDetail());
                    psOrderRouteSectionList.get(i).setStartLng(psOrderRouteSectionList.get(i - 1).getStartLng());
                    psOrderRouteSectionList.get(i).setStartLat(psOrderRouteSectionList.get(i - 1).getStartLat());
                    psOrderRouteSectionList.get(i).setSortNum(psOrderRouteSectionList.get(i).getSortNum()-1);
                } else {
                    psOrderRouteSectionList.get(i).setStartName(psOrderRouteSectionList.get(i - 1).getEndName());
                    psOrderRouteSectionList.get(i).setStartNameDetail(psOrderRouteSectionList.get(i - 1).getEndNameDetail());
                    psOrderRouteSectionList.get(i).setStartLng(psOrderRouteSectionList.get(i - 1).getEndLng());
                    psOrderRouteSectionList.get(i).setStartLat(psOrderRouteSectionList.get(i - 1).getEndLat());
                    psOrderRouteSectionList.get(i).setSortNum(psOrderRouteSectionList.get(i).getSortNum()-1);
                }
            }
        }
        psOrderRouteSectionList.remove(0);

        return R.ok().put("psOrderRouteSectionList",psOrderRouteSectionList);
    }



}
